package cn.echoedu.command;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import cn.echoedu.command.base.ExecuteParam;
import cn.echoedu.command.base.FailureParam;
import cn.echoedu.command.base.SuccessParam;
import cn.echoedu.entity.TelContacts;
import cn.echoedu.util.L;

import com.google.gson.Gson;

public class ContactsClassCommand extends BaseCommand{
   private final String T="ContactsClassCommand--------->";
  


	public void executeSuccess(SuccessParam successparam) {
		// TODO Auto-generated method stub
		List<TelContacts> telcontacts=(List<TelContacts>)successparam.getParam();
		List<ContactsClassEntity> listcontact=new ArrayList<ContactsClassEntity>();
		ContactsClassSuccessResponse contactsmakefriendsuccess=new ContactsClassSuccessResponse();
		for(TelContacts telcontact:telcontacts){
			ContactsClassEntity contactentity=new ContactsClassEntity();
			contactentity.setContact_name(telcontact.getTelcontactsname());
			 L.p(T+"name:"+telcontact.getTelcontactsname());
			contactentity.setContact_number(telcontact.getTel());
			contactentity.setContact_photo_url(telcontact.getImage().getImagesrc());

			listcontact.add(contactentity);
		}
		contactsmakefriendsuccess.setList(listcontact);
		Gson gson=new Gson();
		String jsonstr=gson.toJson(contactsmakefriendsuccess);
		 send(jsonstr,successparam.getResponse());

	}

	public void executeFailure(FailureParam failureparam) {
		// TODO Auto-generated method stub
		 ContactsClassFailureResponse contactclassfailure=new ContactsClassFailureResponse();
	      Gson gson=new Gson();
	      String jsonstr=gson.toJson(contactclassfailure);
	      String sendjson="";
	      try {
	  		sendjson=java.net.URLEncoder.encode(jsonstr,"utf-8");
	  	} catch (UnsupportedEncodingException e1) {
	  		// TODO Auto-generated catch block
	  		e1.printStackTrace();
	  	}
//	  	   java.net.URLDecoder.decode(s)
	  		PrintWriter pw;
	  		try {
	  			failureparam.getResponse().setCharacterEncoding("utf-8");
	  			pw = failureparam.getResponse().getWriter();
	  		    pw.print(sendjson);
	  		} catch (IOException e) {
	  			// TODO Auto-generated catch block
	  			e.printStackTrace();
	  		}
	}
	public void execute(ExecuteParam executeparam) {
		// TODO Auto-generated method stub
		
	}
	
	public class ContactsClassEntity {
		private String contact_name="";
		private String contact_number="";
		private String contact_photo_url="";
		public String getContact_name() {
			return contact_name;
		}
		public void setContact_name(String contact_name) {
			this.contact_name = contact_name;
		}

		public String getContact_photo_url() {
			return contact_photo_url;
		}
		public void setContact_photo_url(String contact_photo_url) {
			this.contact_photo_url = contact_photo_url;
		}
		public String getContact_number() {
			return contact_number;
		}
		public void setContact_number(String contact_number) {
			this.contact_number = contact_number;
		}

	}
	public class ContactsClassSuccessResponse {
		private int code=304;
		private String msg="Class Success";
		private List<ContactsClassEntity> list;
		public int getCode() {
			return code;
		}
		public void setCode(int code) {
			this.code = code;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
		public List<ContactsClassEntity> getList() {
			return list;
		}
		public void setList(List<ContactsClassEntity> list) {
			this.list = list;
		}
	}
	public class ContactsClassFailureResponse {
		private int code=-304;
		private String msg="contact class failure";
		public int getCode() {
			return code;
		}
		public void setCode(int code) {
			this.code = code;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}

	}
	
	
}
